/**
 * Copyright (c) Enalean, 2016 - 2018. All Rights Reserved.
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

$tlp-label-color: #666666;
$tlp-form-element-font-size: 14px;
$tlp-form-element-height: 38px;
$tlp-form-element-padding-horizontal: 12px;
$tlp-form-element-small-font-size: 14px;
$tlp-form-element-small-height: 28px;
$tlp-form-element-small-padding-horizontal: 8px;
$tlp-form-element-large-font-size: 18px;
$tlp-form-element-large-height: 46px;
$tlp-form-element-large-padding-horizontal: 16px;
$tlp-form-element-border-radius: 3px;
$tlp-form-element-border-color: #cccccc;
$tlp-form-element-background: #ffffff;
$tlp-form-element-placeholder-color: #bbbbbb;
$tlp-form-element-border-transition-duration: 150ms;

$tlp-form-element-select-background-size: 8px;
$tlp-form-element-select-small-background-size: 6px;

.tlp-form-element {
    margin: 0 0 $tlp-spacing;

    &:last-child,
    > .tlp-form-element {
        margin: 0;
    }

    > .tlp-text-muted,
    > .tlp-text-info,
    > .tlp-text-success,
    > .tlp-text-warning,
    > .tlp-text-danger {
        margin: 6px 0 0;
        font-size: 13px;
        line-height: 18px;
    }

    &.tlp-form-element-disabled {
        opacity: .5;

        // scss-lint:disable SelectorFormat
        > .tlp-label,
        > .tlp-input,
        > .tlp-search,
        > .tlp-textarea,
        > .tlp-select,
        > .tlp-checkbox,
        > .tlp-checkbox > input,
        > .tlp-radio,
        > .tlp-radio > input,
        > .select2-container.select2-container--disabled .select2-selection--single,
        > .select2-container.select2-container--disabled .select2-selection--multiple,
        > .select2-container.select2-container--disabled .select2-selection__clear {
            cursor: not-allowed;
        }
    }


    &.tlp-form-element-error {
        > .tlp-label {
            color: $tlp-ui-danger;
        }

        // scss-lint:disable SelectorFormat
        // scss-lint:disable SelectorDepth
        > .tlp-input,
        > .tlp-textarea,
        > .tlp-select,
        .select2-container--tlp-select2 .select2-selection--single,
        .select2-container--tlp-select2-small .select2-selection--single,
        .select2-container--tlp-select2-large .select2-selection--single,
        .select2-container--tlp-select2 .select2-selection--multiple,
        .select2-container--tlp-select2-small .select2-selection--multiple,
        .select2-container--tlp-select2-large .select2-selection--multiple,
        .select2-container--tlp-select2 .select2-selection--multiple .select2-selection__rendered .select2-selection__choice,
        .select2-container--tlp-select2 .select2-selection--multiple .select2-selection__rendered .select2-selection__choice .select2-selection__choice__remove:hover::before {
            border-color: $tlp-ui-danger;
            color: $tlp-ui-danger;
        }
    }
}

/**
 * Label
 */
.tlp-label {
    display: block;
    position: relative;
    margin: 0 0 8px;
    color: $tlp-label-color;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;

    > .fa-asterisk {
        position: absolute;
        margin: 0 0 0 3px;
        color: $tlp-ui-danger;
        font-size: 10px;
        vertical-align: text-top;
    }
}

/**
 * Property
 */
.tlp-property {
    margin: 0 0 $tlp-spacing;

    &:last-child {
        margin: 0;
    }

    > .tlp-label {
        cursor: default;
    }
}

.tlp-property-empty {
    color: $tlp-ui-dimmed;
    font-style: italic;
}

/**
 * Inputs
 */
%tlp-form-element {
    display: block;
    width: 100%;
    padding: 0 $tlp-form-element-padding-horizontal;
    transition: border-color $tlp-form-element-border-transition-duration linear;
    border: 1px solid $tlp-form-element-border-color;
    border-radius: $tlp-form-element-border-radius;
    background: $tlp-form-element-background;
    font-family: $tlp-font-family;
    font-size: $tlp-form-element-font-size;

    // scss-lint:disable VendorPrefix
    &::-webkit-input-placeholder {
        color: $tlp-form-element-placeholder-color;
    }

    &::-moz-placeholder {
        color: $tlp-form-element-placeholder-color;
    }

    &:focus {
        transition: border-color $tlp-form-element-border-transition-duration linear;
        border-color: $tlp-theme-color;
    }
}

.tlp-input {
    @extend %tlp-form-element;

    height: $tlp-form-element-height;

    &[size] {
        width: auto;
    }

    &.tlp-input-small {
        height: $tlp-form-element-small-height;
        padding: 0 $tlp-form-element-small-padding-horizontal;
        font-size: $tlp-form-element-small-font-size;
    }

    &.tlp-input-large {
        height: $tlp-form-element-large-height;
        padding: 0 $tlp-form-element-large-padding-horizontal;
        font-size: $tlp-form-element-large-font-size;
    }
}

.tlp-search {
    @extend %tlp-form-element;

    height: $tlp-form-element-height;
    padding: 0 15px 0 33px;
    border-radius: $tlp-form-element-height / 2;
    background: $tlp-form-element-background url('images/tlp-search-background.png') no-repeat;
    background-position: 13px center;

    // scss-lint:disable VendorPrefix
    &::-webkit-search-decoration {
        display: none;
    }

    &[size] {
        width: auto;
    }

    &.tlp-search-small {
        height: $tlp-form-element-small-height;
        padding: 0 10px 0 26px;
        border-radius: $tlp-form-element-small-height / 2;
        background: $tlp-form-element-background url('images/tlp-search-small-background.png') no-repeat;
        background-position: 10px center;
        font-size: $tlp-form-element-small-font-size;
    }

    &.tlp-search-large {
        height: $tlp-form-element-large-height;
        padding: 0 22px 0 40px;
        border-radius: $tlp-form-element-large-height / 2;
        background-position: 20px center;
        font-size: $tlp-form-element-large-font-size;
    }
}

.tlp-textarea {
    @extend %tlp-form-element;

    padding: 8px $tlp-form-element-padding-horizontal;
    overflow: auto;

    &[cols] {
        width: auto;
    }

    &.tlp-textarea-small {
        padding: 6px $tlp-form-element-small-padding-horizontal;
        font-size: $tlp-form-element-small-font-size;
    }

    &.tlp-textarea-large {
        padding: 12px $tlp-form-element-large-padding-horizontal;
        font-size: $tlp-form-element-large-font-size;
    }
}

.tlp-select {
    @extend %tlp-form-element;

    height: $tlp-form-element-height;
    padding: 0 ($tlp-form-element-padding-horizontal * 2 + $tlp-form-element-select-background-size) 0 $tlp-form-element-padding-horizontal;
    background: $tlp-form-element-background url('images/tlp-select-background.png') no-repeat;
    background-position: right $tlp-form-element-padding-horizontal center;
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;

    &.tlp-select-small {
        height: $tlp-form-element-small-height;
        padding: 0 ($tlp-form-element-small-padding-horizontal * 2 $tlp-form-element-select-small-background-size) 0 $tlp-form-element-small-padding-horizontal;
        background: $tlp-form-element-background url('images/tlp-select-small-background.png') no-repeat;
        background-position: right $tlp-form-element-small-padding-horizontal center;
        font-size: $tlp-form-element-small-font-size;
    }

    &.tlp-select-large {
        height: $tlp-form-element-large-height;
        padding: 0 ($tlp-form-element-large-padding-horizontal * 2 + $tlp-form-element-select-background-size) 0 $tlp-form-element-large-padding-horizontal;
        background-position: right $tlp-form-element-large-padding-horizontal center;
        font-size: $tlp-form-element-large-font-size;
    }

    &::-ms-expand {
        display: none;
    }

    &[multiple] {
        height: auto;
        padding: 8px $tlp-form-element-padding-horizontal;
        background: $tlp-form-element-background;

        &.tlp-select-small {
            padding: 6px $tlp-form-element-small-padding-horizontal;
        }

        &.tlp-select-large {
            padding: 12px $tlp-form-element-large-padding-horizontal;
        }
    }

    &.tlp-select-adjusted {
        width: auto;
    }
}

// scss-lint:disable SelectorFormat
// scss-lint:disable NestingDepth
// scss-lint:disable PlaceholderInExtend
%tlp-select2 {
    &.select2-container--disabled .select2-selection--single:focus,
    &.select2-container--disabled .select2-selection--multiple:focus {
        border-color: $tlp-form-element-border-color;
    }

    &.select2-container--open {
        &.select2-container--above .select2-selection--single,
        &.select2-container--above .select2-selection--multiple {
            border-radius: 0 0 $tlp-form-element-border-radius $tlp-form-element-border-radius;
            border-color: $tlp-theme-color;
        }

        &.select2-container--below .select2-selection--single,
        &.select2-container--below .select2-selection--multiple {
            border-radius: $tlp-form-element-border-radius $tlp-form-element-border-radius 0 0;
            border-color: $tlp-theme-color;
        }

        .select2-dropdown--above {
            border-radius: $tlp-form-element-border-radius $tlp-form-element-border-radius 0 0;
        }

        .select2-dropdown--below {
            border-radius: 0 0 $tlp-form-element-border-radius $tlp-form-element-border-radius;
        }
    }

    .select2-selection--single {
        @extend %tlp-form-element;

        display: flex;
        align-items: center;
        height: $tlp-form-element-height;
        padding: 0 ($tlp-form-element-padding-horizontal * 2 + $tlp-form-element-select-background-size) 0 $tlp-form-element-padding-horizontal;
        background: $tlp-form-element-background url('images/tlp-select-background.png') no-repeat;
        background-position: right $tlp-form-element-padding-horizontal center;
        cursor: default;

        .select2-selection__rendered {
            width: 100%;
            padding: 0;
            line-height: normal;

            .select2-selection__placeholder {
                color: $tlp-form-element-placeholder-color;
            }
        }
    }

    .select2-selection--multiple {
        @extend %tlp-form-element;

        min-height: $tlp-form-element-height;
        padding: 0 $tlp-form-element-padding-horizontal 5px 6px;

        .select2-selection__rendered {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            width: 100%;
            height: 100%;
            padding: 0;

            .select2-selection__choice {
                @extend %tlp-badge;
                @extend .tlp-badge-primary.tlp-badge-outline;

                flex: 0 0 auto;
                margin: 5px 5px 0 0;

                .select2-selection__choice__remove {
                    display: inline-block;
                    width: 12px;
                    margin: 0 2px 0 0;
                    font-size: 0;
                    line-height: 0;

                    &::before {
                        font-family: 'FontAwesome';
                        font-size: 12px;
                        font-weight: 400;
                        text-align: center;
                        content: '\f00d'; // fa-remove
                    }

                    &:hover::before {
                        width: 12px;
                        color: $tlp-theme-color;
                        content: '\f057'; // fa-times-circle
                    }
                }
            }

            .select2-search--inline {
                flex: 1 1 auto;
                order: 2;
                list-style-type: none;

                .select2-search__field {
                    margin: 9px 0 0;
                    background: transparent;
                    font-family: $tlp-font-family;
                }
            }

            .select2-selection__clear {
                flex: 0 0 auto;
                order: 3;
                margin: 4px 0 0;
            }
        }
    }

    .select2-selection__clear {
        float: right;
        font-size: 18px;
        line-height: 17px;
        cursor: pointer;
    }

    .select2-dropdown {
        padding: 0;
        border-color: $tlp-theme-color;
        box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
        z-index: 100051;

        .select2-search--dropdown {
            padding: $tlp-form-element-padding-horizontal;

            .select2-search__field {
                @extend %tlp-form-element;

                height: $tlp-form-element-small-height;
                padding: 0 4px;
                font-size: $tlp-form-element-small-font-size;
            }
        }

        .select2-results__options {
            max-height: 250px;
            overflow-y: auto;
        }

        .select2-results__option {
            padding: 6px $tlp-form-element-padding-horizontal;
            font-size: $tlp-form-element-small-font-size;
            line-height: normal;

            &[role="group"] {
                margin: $tlp-form-element-padding-horizontal 0 0;
                padding: 0;

                &:first-child {
                    margin: 0;
                }

                .select2-results__group {
                    padding: 6px $tlp-form-element-padding-horizontal;
                    color: $tlp-ui-dimmed;
                    font-size: $tlp-form-element-small-font-size;
                    font-weight: 600;
                    text-transform: uppercase;
                }

                .select2-results__options--nested > .select2-results__option {
                    padding: 6px $tlp-form-element-padding-horizontal 6px $tlp-form-element-padding-horizontal * 2;

                    &[aria-selected="true"] {
                        padding: 6px $tlp-form-element-padding-horizontal 6px 44px;

                        &::before {
                            left: $tlp-form-element-padding-horizontal * 2;
                        }
                    }
                }
            }

            &.select2-results__option--highlighted {
                background: $tlp-theme-color;
                color: $tlp-ui-white;
            }

            &[aria-selected="true"] {
                position: relative;
                padding: 6px $tlp-form-element-padding-horizontal 6px 32px;
                color: $tlp-theme-color;

                &::before {
                    position: absolute;
                    top: 8px;
                    left: $tlp-form-element-padding-horizontal;
                    font-family: 'FontAwesome';
                    content: '\f00c'; // fa-check
                }

                &.select2-results__option--highlighted {
                    color: $tlp-ui-white;
                }
            }

            &[aria-disabled] {
                color: $tlp-ui-dimmed;
                cursor: not-allowed;
            }

            &.select2-results__message,
            &.loading-results {
                padding: $tlp-form-element-padding-horizontal;
                color: $tlp-ui-dimmed;
            }
        }

        .select2-search--hide + .select2-results .select2-results__option:first-child[role="group"] {
            padding: $tlp-form-element-padding-horizontal 0 0;
        }
    }
}

// scss-lint:disable QualifyingElement
span.select2-container--tlp-select2 {
    @extend %tlp-select2;
}

span.select2-container--tlp-select2-small {
    @extend %tlp-select2;

    .select2-selection--single {
        height: $tlp-form-element-small-height;
        padding: 0 ($tlp-form-element-small-padding-horizontal * 2 + $tlp-form-element-select-small-background-size) 0 $tlp-form-element-small-padding-horizontal;
        background: $tlp-form-element-background url('images/tlp-select-small-background.png') no-repeat;
        background-position: right $tlp-form-element-small-padding-horizontal center;
        font-size: $tlp-form-element-small-font-size;
    }

    .select2-selection--multiple {
        min-height: $tlp-form-element-small-height;
        padding: 0 $tlp-form-element-small-padding-horizontal;

        .select2-selection__rendered {
            .select2-selection__choice {
                margin: 1px 3px 0 0;
                padding: 2px 8px;
            }

            .select2-selection__clear {
                margin: 0;
            }

            .select2-search--inline .select2-search__field {
                margin: 4px 0 0;
            }
        }
    }
}

span.select2-container--tlp-select2-large {
    @extend %tlp-select2;

    .select2-selection--single {
        height: $tlp-form-element-large-height;
        padding: 0 ($tlp-form-element-large-padding-horizontal * 2 + $tlp-form-element-select-background-size) 0 $tlp-form-element-large-padding-horizontal;
        background-position: right $tlp-form-element-large-padding-horizontal center;
        font-size: $tlp-form-element-large-font-size;

        .select2-selection__rendered .select2-selection__clear {
            font-size: 22px;
            line-height: 20px;
        }
    }

    .select2-selection--multiple {
        min-height: $tlp-form-element-large-height;
        padding: 0 $tlp-form-element-large-padding-horizontal;

        .select2-selection__rendered {
            padding: 6px 0;

            .select2-selection__choice {
                margin: 2px 8px 2px 0;
                padding: 7px 12px;
                font-size: $tlp-form-element-large-font-size;
                font-weight: 400;

                .select2-selection__choice__remove {
                    position: relative;
                    top: -1px;
                    margin: 0 4px 0 0;

                    &::before {
                        font-size: 14px;
                    }
                }
            }

            .select2-selection__clear {
                margin: 0;
                font-size: 22px;
                line-height: 17px;
            }

            .select2-search--inline .select2-search__field {
                margin: 3px 0 0;
                font-size: $tlp-form-element-large-font-size;
            }
        }
    }
}

.tlp-checkbox {
    line-height: 14px;
}

.tlp-checkbox > input,
.tlp-radio > input {
    margin: 0 3px 0 0;
    vertical-align: bottom;
}

/**
 * Prepend, append
 */
.tlp-form-element-prepend,
.tlp-form-element-append {
    display: flex;
    align-items: center;

    > .tlp-prepend:not([class*="tlp-button"]):not([class*="tlp-dropdown"]),
    > .tlp-append:not([class*="tlp-button"]):not([class*="tlp-dropdown"]) {
        display: flex;
        align-items: center;
        height: $tlp-form-element-height;
        padding: 0 $tlp-form-element-padding-horizontal;
        transition: border-color $tlp-form-element-border-transition-duration linear;
        border: 1px solid $tlp-form-element-border-color;
        background: transparentize($tlp-theme-color, .95);
        color: $tlp-theme-color;
        font-size: $tlp-form-element-font-size;
        font-weight: 600;
        vertical-align: middle;

        &.tlp-prepend-small,
        &.tlp-append-small {
            height: $tlp-form-element-small-height;
            padding: 0 $tlp-form-element-small-padding-horizontal;
            font-size: $tlp-form-element-small-font-size;
        }

        &.tlp-prepend-large,
        &.tlp-append-large {
            height: $tlp-form-element-large-height;
            padding: 0 $tlp-form-element-large-padding-horizontal;
            font-size: $tlp-form-element-large-font-size;
        }
    }

    > span.select2-container--tlp-select2 .select2-selection--single {
        padding: 0 $tlp-form-element-padding-horizontal;
        background: none;
    }

    > span.select2-container--tlp-select2-small .select2-selection--single {
        padding: 0 $tlp-form-element-small-padding-horizontal;
        background: none;
    }

    > span.select2-container--tlp-select2-large .select2-selection--single {
        padding: 0 $tlp-form-element-large-padding-horizontal;
        background: none;
    }
}

.tlp-form-element-prepend {
    > .tlp-prepend {
        z-index: 1;
        flex: 0 0 auto;
        order: 1;
        margin-right: -1px;
        border-radius: 0;

        &:first-child,
        &:first-child > button[class*="tlp-button"] {
            border-radius: $tlp-form-element-border-radius 0 0 $tlp-form-element-border-radius;
        }
    }

    > .tlp-input {
        order: 2;
        min-width: 0;
        border-radius: 0 $tlp-form-element-border-radius $tlp-form-element-border-radius 0;
    }

    > .tlp-input:not(:first-child):not(:last-child) {
        border-radius: 0;
    }

    > span.select2-container--tlp-select2,
    > span.select2-container--tlp-select2-small,
    > span.select2-container--tlp-select2-large {
        order: 2;

        .select2-selection--single,
        .select2-selection--multiple {
            border-radius: 0 $tlp-form-element-border-radius $tlp-form-element-border-radius 0;
        }

        &.select2-container--open {
            &.select2-container--above .select2-selection--single,
            &.select2-container--above .select2-selection--multiple {
                border-radius: 0 0 $tlp-form-element-border-radius;
            }

            &.select2-container--below .select2-selection--single,
            &.select2-container--below .select2-selection--multiple {
                border-radius: 0 $tlp-form-element-border-radius 0 0;
            }
        }
    }
}

.tlp-form-element-append {
    > .tlp-append {
        z-index: 1;
        flex: 0 0 auto;
        order: 2;
        margin-left: -1px;
        border-radius: 0;

        &:last-child,
        &:last-child > button[class*="tlp-button"] {
            border-radius: 0 $tlp-form-element-border-radius $tlp-form-element-border-radius 0;
        }
    }

    > .tlp-input,
    > .tlp-select {
        order: 1;
        min-width: 0;
        border-radius: $tlp-form-element-border-radius 0 0 $tlp-form-element-border-radius;
    }

    > span.select2-container--tlp-select2,
    > span.select2-container--tlp-select2-small,
    > span.select2-container--tlp-select2-large {
        .select2-selection--single,
        .select2-selection--multiple {
            border-radius: $tlp-form-element-border-radius 0 0 $tlp-form-element-border-radius;
        }

        &.select2-container--open {
            &.select2-container--above .select2-selection--single,
            &.select2-container--above .select2-selection--multiple {
                border-radius: 0 0 0 $tlp-form-element-border-radius;
            }

            &.select2-container--below .select2-selection--single,
            &.select2-container--below .select2-selection--multiple {
                border-radius: $tlp-form-element-border-radius 0 0;
            }
        }
    }
}
